package com.ysstech.common.service;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysstech.common.entity.Logger;
import com.ysstech.common.entity.PageBean;
import com.ysstech.common.mapper.LoggerMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Configuration;

@Configuration
public class LoggerService extends ServiceImpl<LoggerMapper, Logger> {

    /**
     * 获取日志信息
     * @param logger
     * @param pageBean
     * @return
     * @throws Exception
     */
    public PageBean<Logger> queryAllLogger(Logger logger, PageBean pageBean) throws Exception {
        QueryWrapper<Logger> wrapper = this.getQueryWrapper(logger);
        IPage pageScheduleLog = this.page(new Page<>(pageBean.getPageNum(), pageBean.getPageSize()), wrapper);
        this.switchedListLogger(pageScheduleLog.getRecords());
        return new PageBean<>(pageScheduleLog);
    }

    /**
     * 类型，机构等中文的转换
     *
     * @param list
     * @return
     * @throws Exception
     */
    private List<Logger> switchedListLogger(List<Logger> list) throws Exception {
        for (Logger scheduleLog : list) {
            if (null == scheduleLog) {
                continue;
            }
        }
        return list;
    }

    /**
     * 查询条件
     *
     * @param logger
     * @return
     * @throws Exception
     */
    private QueryWrapper<Logger> getQueryWrapper(Logger logger) throws Exception {
        QueryWrapper<Logger> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("ADD_TIME");
        if (null == logger) {
            return wrapper;
        }
        if (StringUtils.isNotBlank(logger.getMethodDescription())) {
            wrapper.like("METHOD_DESCRIPTION", logger.getMethodDescription());
        }
        if (null != logger.getStatus()) {
            wrapper.eq("STATUS", logger.getStatus());
        }
        return wrapper;
    }
}
